package com.zzstm.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzstm.commonUtils.Base64EnCodeAndDecode;
import com.zzstm.eduservice.entity.EduComment;
import com.zzstm.eduservice.mapper.EduCommentMapper;
import com.zzstm.eduservice.service.EduCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-11-19
 */
@Service
public class EduCommentServiceImpl extends ServiceImpl<EduCommentMapper, EduComment> implements EduCommentService {

    //根据课程id分页查询评论
    @Override
    public Map<String, Object> getPageComment(Page<EduComment> page, String courseId) {
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.orderByDesc("gmt_create");
        baseMapper.selectPage(page,wrapper);
        if (page.getRecords().size() == 0){
            return null;
        }
        List<EduComment> records = page.getRecords();
        //将昵称解码
        String nickname;
        for (int i = 0; i < records.size(); i++) {
            EduComment comment = records.get(i);
            nickname = comment.getNickname();
            nickname = Base64EnCodeAndDecode.decode(nickname);
            comment.setNickname(nickname);
        }
        System.err.println(records.get(0));
        Map<String,Object> map = new HashMap<>();
        map.put("records",records);
        map.put("current",page.getCurrent());
        map.put("pages",page.getPages());
        map.put("size",page.getSize());
        map.put("total",page.getTotal());
        map.put("hasNext",page.hasNext());
        map.put("hasPrevious",page.hasPrevious());
        return map;
    }
}
